﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using log4net;

namespace Chowpoo.Helper
{
    public class Email
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(Email));

        public static bool SendMail(string smtpHost, string userName, string pwd, bool bSsl, MailAddress from, List<string> toList,
                             string sCc, string sSubject, string sBody, Encoding eCoding, Attachment[] attachments)
        {
            return SendMail(smtpHost, userName, pwd, bSsl, from, toList,
                     sCc, sSubject, sBody, eCoding, attachments, false, null, null);
        }

        public static bool SendMail(string smtpHost, string userName, string pwd, bool bSsl, MailAddress from, List<string> toList,
                             string sCc, string sSubject, string sBody, Encoding eCoding, Attachment[] attachments, bool isAsync, object item, SendCompletedEventHandler completeEvent)
        {
            bool flag;
            try
            {
                var client = new SmtpClient();
                if (!string.IsNullOrEmpty(smtpHost))
                {
                    client.Host = smtpHost;
                }
                if (!(string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(pwd)))
                {
                    client.Credentials = new NetworkCredential(userName, pwd);
                }
                else if (!string.IsNullOrEmpty(pwd))
                {
                    var credentials = client.Credentials as NetworkCredential;
                    if (credentials == null)
                        throw new NullReferenceException("邮箱验证失败！");
                    credentials.Password = pwd;
                }
                client.EnableSsl = bSsl;
                var message = new MailMessage
                {
                    Body = sBody,
                    Subject = sSubject,
                    From = from
                };
                foreach (var itemStr in toList)
                {
                    message.To.Add(itemStr);
                }

                //完成事件加载
                client.SendCompleted += completeEvent;

                if (!string.IsNullOrEmpty(sCc))
                {
                    string[] strArray = sCc.Split(",;|".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                    foreach (var str in strArray)
                    {
                        message.CC.Add(str);
                    }
                }
                message.SubjectEncoding = eCoding;
                message.BodyEncoding = eCoding;
                message.IsBodyHtml = true;
                if ((attachments != null) && (attachments.Length > 0))
                {
                    foreach (var attachment in attachments)
                    {
                        message.Attachments.Add(attachment);
                    }
                }
                try
                {
                    if (isAsync)
                        client.SendAsync(message, item);
                    else
                        client.Send(message);
                    return true;
                }
                catch (Exception ex)
                {
                    log.Error("邮件发送错误！", ex);
                    return false;
                }
            }
            catch (Exception ex)
            {
                log.Error("邮件发送错误！", ex);
                flag = false;
            }
            return flag;
        }
    }
}
